#include <atomic>
#include "ConcurrentAlloc.h"
using std::cout;
using std::cin;
using std::endl;


void BenchmarkMalloc(size_t ntimes, size_t nworks, size_t rounds)
{
	std::vector<std::thread> vthread(nworks);
	std::atomic<size_t> malloc_costtime = 0;
	std::atomic<size_t> free_costtime = 0;
	for (size_t k = 0; k < nworks; ++k)
	{
		vthread[k] = std::thread([&, k]() {
			std::vector<void*> v;
			v.reserve(ntimes);
			for (size_t j = 0; j < rounds; ++j)
			{
				size_t begin1 = clock();
				for (size_t i = 0; i < ntimes; i++)
				{
					//v.push_back(malloc(16));
					v.push_back(malloc((16 + i) % 8192 + 1));
				}
				size_t end1 = clock();
				size_t begin2 = clock();
				for (size_t i = 0; i < ntimes; i++)
				{
					free(v[i]);
				}
				size_t end2 = clock();
				v.clear();
				malloc_costtime += (end1 - begin1);
				free_costtime += (end2 - begin2);
			}
			});
	}
	for (auto& t : vthread)
	{
		t.join();
	}

	cout << nworks << "个线程并发执行 " << rounds << "轮次，每轮次malloc " << ntimes << "次: 花费： " << malloc_costtime << " ms\n";
	cout << nworks << "个线程并发执行 " << rounds << "轮次，每轮次free " << ntimes << "次: 花费： " << free_costtime << " ms\n";
	cout << nworks << "个线程并发执行 " << rounds << "malloc&free %u次，总计花费：" << ntimes << "次: 总计花费：" << malloc_costtime + free_costtime << " ms\n";


}

// 单轮次申请释放次数 线程数 轮次
void BenchmarkConcurrentMalloc(size_t ntimes, size_t nworks, size_t rounds)
{
	std::vector<std::thread> vthread(nworks);
	std::atomic<size_t> malloc_costtime = 0;
	std::atomic<size_t> free_costtime = 0;
	for (size_t k = 0; k < nworks; ++k)
	{
		vthread[k] = std::thread([&]() {
			std::vector<void*> v;
			v.reserve(ntimes);
			for (size_t j = 0; j < rounds; ++j)
			{
				size_t begin1 = clock();
				for (size_t i = 0; i < ntimes; i++)
				{
					/*if (j == 2&& i==24)
					{
						int sssss = 0;
						int tt =0 ;
						sssss = tt;
					}
					cout << i << " ";*/

					//v.push_back(ConcurrentAlloc(16));
					v.push_back(ConcurrentAlloc((16 + i) % 8192 + 1));

					
				}
				size_t end1 = clock();
				size_t begin2 = clock();
				for (size_t i = 0; i < ntimes; i++)
				{
					ConcurrentFree(v[i]);
				}
				size_t end2 = clock();
				v.clear();
				malloc_costtime += (end1 - begin1);
				free_costtime += (end2 - begin2);
			}
			});
	}
	for (auto& t : vthread)
	{
		t.join();
	}



	cout << nworks << "个线程并发执行 " << rounds << "轮次，每轮次concurrent alloc " << ntimes << "次: 花费： " << malloc_costtime << " ms\n";
	cout << nworks << "个线程并发执行 " << rounds << "轮次，每轮次concurrent dealloc " << ntimes << "次: 花费： " << free_costtime << " ms\n";
	cout << nworks << "个线程并发执行 " << rounds << "个线程并发concurrent alloc&dealloc " << ntimes << "次: 总计花费：" << malloc_costtime + free_costtime << " ms\n";
}


int main()
{


	//Span* span = NewAlloc<Span>();
	//span->_isUsed = 0;


	//span = NewAlloc<Span>();
	//span->_isUsed = 0;


	//int* ptr = (int*)NewAlloc(16);
	//*ptr = 10;
	//*(ptr+1) = 12;
	//*(ptr+2) = 14;
	//*(ptr+3) = 15;


	//TCMalloc_PageMap3<Span, 64 - PAGE_SHIFT > _idSpanMap;

	//Span span;
	//_idSpanMap.set(222, &span);
	//_idSpanMap.set(2232, &span);
	//_idSpanMap.set(22312, &span);








	size_t n = 60000;
	cout << "==========================================================" << endl;
	BenchmarkConcurrentMalloc(n, 5, 10);
	//cout << endl << endl;
	////BenchmarkMalloc(n, 10, 10);
	//cout << "==========================================================" <<endl;
	//return 0;


}